/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tcloudclient.settings;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import tcloudclient.contex.Context;
import tcloudclient.fileSystem.FileNode;

/**
 *
 * @author Tomas
 */
public class XmlLoaderSaver {

    private static <V> void save(String path, V object, Class<V> clazz) {
        try {

            File file = new File(path);
            JAXBContext jaxbContext = JAXBContext.newInstance(clazz);
            Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

            // output pretty printed
            jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

            jaxbMarshaller.marshal(object, file);
            jaxbMarshaller.marshal(object, System.out);

        } catch (JAXBException e) {
            e.printStackTrace();
        }

    }

    private static <V> V load(String path, Class<V> clazz) throws JAXBException {
        try {
            File file = new File(path);
            if(!file.exists()){
                return null;
            }
            JAXBContext jaxbContext = JAXBContext.newInstance(clazz);

            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
            V object = (V) jaxbUnmarshaller.unmarshal(file);
            //System.out.println(object);
            return object;

        } catch (JAXBException e) {
            e.printStackTrace();
            throw e;
        }
    }

    public static void saveAppSettings() {
        File current = new File(".");
        ApplicationSettings setting = Context.getInstance().getApplicationSettings();
        File settingsFile = new File(current.getAbsolutePath() + "/settings/");
        if (!settingsFile.exists()) {
            settingsFile.mkdir();
        }
        save(current.getAbsolutePath() + "/settings/AppSetings.xml", setting, ApplicationSettings.class);
    }

    public static void loadAppSettings() {
        File current = new File(".");
        File settingsFile = new File(current.getAbsolutePath() + "/settings/");
        if (settingsFile.exists()) {
            ApplicationSettings loadedSet;
            try {
                loadedSet = load(settingsFile.getAbsolutePath() + "/AppSetings.xml", ApplicationSettings.class);
                Context.getInstance().setApplicationSettings(loadedSet);
            } catch (JAXBException ex) {
                Logger.getLogger(XmlLoaderSaver.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public static void saveFileSystemStatus() {
        File current = new File(".");
        FileNode rootFolder = Context.getInstance().getRootFolder();
        File settingsFile = new File(current.getAbsolutePath() + "/settings/");
        if (!settingsFile.exists()) {
            settingsFile.mkdir();
        }
        save(current.getAbsolutePath() + "/settings/FsStatus.xml", rootFolder, FileNode.class);
    }

    public static FileNode loadFileSystemStatus() {
        File current = new File(".");
        File settingsFile = new File(current.getAbsolutePath() + "/settings/");
        FileNode loadedStatus;
        if (settingsFile.exists()) {            
            try {
                loadedStatus = load(settingsFile.getAbsolutePath() + "/FsStatus.xml", FileNode.class);
                Context.getInstance().setRootFolder(loadedStatus);
                return loadedStatus;
            } catch (JAXBException ex) {
                Logger.getLogger(XmlLoaderSaver.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return null;
    }
    
    public static void saveLoginSetting(AutoLoginSetting setting){
        File current = new File(".");
        File settingsFile = new File(current.getAbsolutePath() + "/settings/");
        if(!settingsFile.exists()){
            settingsFile.mkdir();
        }
        save(settingsFile.getAbsolutePath() + "/LoginSetting.xml", setting, AutoLoginSetting.class);
    }
    
    public static AutoLoginSetting loadLoginSetting(){
        File current = new File(".");
        File settingsFile = new File(current.getAbsolutePath() + "/settings/LoginSetting.xml");
        AutoLoginSetting loginSetting;
        if (settingsFile.exists()) {            
            try {
                loginSetting = load(settingsFile.getAbsolutePath(), AutoLoginSetting.class);
                return loginSetting;
            } catch (JAXBException ex) {
                Logger.getLogger(XmlLoaderSaver.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return null;
    }
}
